///////////////////////////////////////////////////////////////////////////////
//	FILENAME:	eTNoteUI.m
//	SUMMARY:	Implementation of the inspector of Notes
//	SUPERCLASS:	Object
//	INTERFACE:	eTNoteUI.nib
//	PROTOCOLS:	<Inspectable>
//	AUTHOR:		Rohit Khare
//	COPYRIGHT:	(c) 1994 California Institure of Technology, eText Project
///////////////////////////////////////////////////////////////////////////////
//  IMPLEMENTATION COMMENTS
//		Just a shared nib, slip-slidin' awaaay.
///////////////////////////////////////////////////////////////////////////////
//	HISTORY
//	07/24/94:	Migrated to subclas of eTImageUI 
//	05/22/94:	Created. First actual implementation.
///////////////////////////////////////////////////////////////////////////////

#import "eTNoteUI.h"
#define PROP "Notation"

@implementation eTNoteUI
//	id		notePanel;
//	id		noteView;
//	id		etNote;
//	id		repText
//	id		ownerField;
//	id		dateField;
// 	BOOL	ownerDirty;
//	BOOL	textDirty;

+ new
{
	static eTNoteUI *ui = nil;
	
	if (!ui) {
		ui = [[eTNoteUI alloc] init];
	}
	return ui;
}

- init
{
	char        buf[MAXPATHLEN];
	NXBundle   *bundle;

	[super init];
	bundle = [NXBundle bundleForClass:[eTNoteUI class]];
	if ( [bundle getPath:buf forResource:"eTNoteUI" ofType:"nib"] ) {
		[NXApp loadNibFile:buf owner:self withNames:NO];
	} else {
		NXLogError("NIB not found: eTNoteUI");
	}
	noteView = [notePanel contentView];
	[repText setDelegate:self];
	ownerDirty = textDirty = NO;
	return self;
}

- free {return self;}

- setNote:newNote
{
	etNote = newNote;
	return [self load];
}

- load
{
	NXStream *stream;
	char	*buf;

	[ownerField setStringValue:[etNote owner]];	
	[dateField setStringValue:[etNote date]];
	buf = (char *)[etNote RTFRep];
	if (buf) {stream = NXOpenMemory(buf,strlen(buf), NX_READONLY);
	[repText readRichText:stream];
	NXClose(stream);}
	return self;
}
- resetOwner:sender
{
	if (ownerDirty) {
		[etNote setOwner:NXUniqueString([sender stringValue])];
		ownerDirty= NO;
	}
	return self;
}

- resetText:sender
{
	NXStream	*stream;
	char		*buffer, *data;
	int			length, maxlen;

	if (!textDirty) return self;
	stream = NXOpenMemory(NULL, 0, NX_WRITEONLY);
	[repText writeRichText:stream];
	NXGetMemoryBuffer(stream, &buffer, &length, &maxlen);
	data = malloc((length+1) * sizeof(char));
	bcopy(buffer, data, length);
	data[length] = 0;
	NXCloseMemory(stream, NX_FREEBUFFER);
	[etNote setRTFRep:data];
	free(data);
	[dateField setStringValue:[etNote date]];
	textDirty = NO;
	return self;
}

- sync
{
	[self resetText:repText];
	[self resetOwner:ownerField];
	return self;
}
- textDidGetKeys:textObject isEmpty:(BOOL)flag
{
	if (textObject == repText)	textDirty = YES;
	else ownerDirty = YES;
	return self;
}
- textDidChange:textObject
{
	if (textObject == repText)	textDirty = YES;
	else ownerDirty = YES;
	return self;
}

//////////////////////
- (const NXAtom *) types
{
	static NXAtom *types = NULL;
	
	if (!types) {
		int i;
		const NXAtom *superTypes = [super types];
		
		for(i=0;superTypes[i];i++);
		i++; // make room for terminating NULL
		i++; // make room for our type
		types = malloc(i * sizeof(NXAtom));
		types[0] = NXUniqueString(PROP);
		for(i=0;superTypes[i];i++) types[i+1] = superTypes[i];
		types[i+1] = NULL;
	}
	return types;
}

- (const char *) inspectorTitle
{
	return NXUniqueString("eTNote");
}

- resignInspector: (View *) oldInspector ofType: (const char *) type
{
	if (oldInspector != noteView)
		[super resignInspector:oldInspector ofType:type];
	else 
		[self resetText:repText];
	return self;
}

- activateInspector: (View *) newInspector ofType: (const char *) type
{
	if (newInspector != noteView)
		[super activateInspector:newInspector ofType:type];
	else 
		[[NXApp inspector] makeFirstResponder:repText];
	return self;
}

- inspectorForType:(const char *) type
{
	if(!strcmp(type,NXUniqueString(PROP)))
		return noteView;
	return [super inspectorForType:type];
}

///////////////////////////////
@end